﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
public partial class Order_NewOrder : System.Web.UI.Page, ICallbackEventHandler
{
    private string _wlgs;
    public string WLGS
    {
        get { return _wlgs; }
        set { _wlgs = value; }
    }
    public string result=",false";
    List<Model.PAYDETAIL> PayLists = new List<Model.PAYDETAIL>();//支会信息
    List<Model.BUYPOCKETDETAIL> BuyLists = new List<Model.BUYPOCKETDETAIL>(); //购物信息
    Model.V_CUSTOMER_ADDRESS address = new Model.V_CUSTOMER_ADDRESS(); //送货地址
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Attributes.Add("onkeydown", "SubmitKeyClick();");
        if (Request.QueryString["accountcode"] != null && Request.QueryString["zk"] != null)
        {
            //TextBox2.Attributes.Add("onblur", "AddressInit('" + Request.QueryString["accountcode"].ToString().Trim() + "')");
            TextBox2.Text = Request.QueryString["accountcode"].ToString().Trim();
            TextBox1.Text = Request.QueryString["zk"].ToString().Trim();
            TextBox3.Text = Request.QueryString["gkmc"].ToString().Trim();
            TextBox4.Text = Request.QueryString["nike"].ToString().Trim();
        }
        if (!IsPostBack)
        {
            Init_WLGS();
        }

        Session["FCode"] = "400101";
    }

    #region 生成订单
    public void RaiseCallbackEvent(string eventArgument)
    {

        try
        {

            //主表信息
            string[] tmp = eventArgument.Split('|');
            string[] mmm = tmp[0].Split(',');
            string gkdm = mmm[0];
            address.Receiver = mmm[1];
            address.Address = mmm[2];
            address.Tel = mmm[3];
            address.Mobile = mmm[4];
            address.PostDM = mmm[5];
            string jylx = mmm[6];
            string ddly = mmm[7];
            string fyxz = mmm[8];
            string fp = mmm[9];
            string rlje = mmm[10];
            string wlfs = mmm[11];
            string wlfy = mmm[12];
            string bz = mmm[13];
            string isFp = mmm[14];
            string tt = mmm[15];
            string province = mmm[16];
            string city = mmm[17];
            string area = mmm[18];
            string ddcxdh = mmm[19];
            string orderzk = mmm[20];
            decimal payAmount = 0.00M;

            Model.V_CUSTOMER customer = new BLL.V_CUSTOMER().GetModel(gkdm);
            if (customer == null)
            {
                result = "系统内不存在该客户！,false";
                return;
            }

            //商品明细
            string[] t = tmp[1].Split('#');
            for (int i = 0; i < t.Length; i++)
            {
                string[] a = t[i].Split(',');
                string spdm = a[0];
                string size = a[2];
                string color = a[1];
                Decimal sl = Decimal.Parse(a[3]);
                //可用库存不足时加提示. [是]继续[否]取消；



                //string spzs = new BLL.SPKCB().OC_Product_StockCount("spkcb.spdm='" + spdm+"'", "1");
                //if (sl > Decimal.Parse(spzs))
                //{
                //    kykc = false;
                //}

                Decimal price = Decimal.Parse(a[4]);
                Decimal zk = Decimal.Parse(a[5]);
                Decimal weight = Decimal.Parse(a[6]);
                string bpd_id = a[7];
                string cxdh = a[8];
                string yhfs = a[9];
                string parent = a[10];
                Decimal result=0.00M;
                Decimal aprice = Decimal.TryParse(a[11],out result)==true?result:0.00M;
                Model.BUYPOCKETDETAIL bd = new Model.BUYPOCKETDETAIL();
                bd.BPD_ID = int.Parse(bpd_id);
                bd.BPD_USERCODE = gkdm;
                bd.BPD_COMMCODE = spdm;
                bd.BPD_COMMCOLOR = color;
                bd.BPD_COMMSIZE = size;
                bd.BPD_DISC = zk;
                bd.BPD_QTY = sl;
                bd.BPD_SPRICE = price;
                bd.BPD_APRICE = aprice;
                bd.BPD_SAMT = sl * price;
                bd.BPD_AAMT = sl * bd.BPD_APRICE;
                bd.BPD_WEIGHT = weight;
                bd.BPD_ADDITG = bd.BPD_AAMT * new BLL.V_VIPSET().GetBaseBS(gkdm);
                if (parent == "-1")
                {
                    bd.BPD_FIELD1 = cxdh;
                    bd.BPD_FIELD2 = "0";                    
                }
                else
                {
                    bd.BPD_FIELD1 = parent;
                    bd.BPD_FIELD2 = "1";
                }

                BuyLists.Add(bd);
            }
            if (tmp.Length > 2)
            {
                //支付明细
                string[] z = tmp[2].Split('#');
                for (int r = 0; r < z.Length; r++)
                {
                    int defaultid = r + 1;
                    string[] zz = z[r].Split(',');
                    string zhanghao = zz[0];
                    string zfdm = zz[1];
                    string zfje = zz[2];
                    string zf_jylx = zz[3];
                    string jyh = zz[4];
                    string jyrq = zz[5];
                    string bzz = zz[6];
                    Model.PAYDETAIL paydetail = new Model.PAYDETAIL();  //支付明细
                    paydetail.PD_DETAILID = defaultid.ToString();
                    paydetail.PD_ACCOUNT = zhanghao;
                    paydetail.PD_PAYTYPE = zfdm;
                    paydetail.PD_PAYAMT = zfje.Trim() == "" ? 0.00M : Decimal.Parse(zfje);
                    paydetail.PD_PAYDATE = jyrq.Trim() == "" ? DateTime.MinValue : DateTime.Parse(jyrq);
                    paydetail.PD_DEALID = jyh;
                    paydetail.PD_DEALDATE = jyrq.Trim() == "" ? DateTime.MinValue : DateTime.Parse(jyrq);
                    paydetail.PD_NOTE = bzz;
                    paydetail.PD_DTCODE = zf_jylx;
                    PayLists.Add(paydetail);

                    payAmount += Decimal.Parse(zfje);
                }
            }
            #region
            decimal Order_SL = 0;//总数量


            decimal Order_ZK = Decimal.Parse(orderzk);//存储会员购物时折扣值.
            decimal ORDER_AMT = 0; //取订单商品明细实际金额合计


            decimal ORDER_OFFERAMT = Decimal.Parse(rlje);//让利金额
            decimal ORDER_AAMT = 0;//金额 - 让利金额，不可编辑



            decimal WLFY = Decimal.Parse(wlfy);//物流费用
            int BPD_ITGBUY = 0;//积分换购标志
            decimal SYJF = 0;//使用积分
            int FP = Int32.Parse(fp);//地否需要发票



            string FPTT = "";//发票抬头
            string CXFS = "";//促销方式
            string CXDH = ddcxdh;//促销单号
            foreach (Model.BUYPOCKETDETAIL buyinfo in BuyLists)
            {
                Order_SL = Order_SL + buyinfo.BPD_QTY;
                ORDER_AMT = ORDER_AMT + buyinfo.BPD_AAMT;
                ORDER_OFFERAMT = ORDER_OFFERAMT;
                if (buyinfo.BPD_ITGBUY == 1)
                {
                    BPD_ITGBUY = 1;
                    SYJF = SYJF + buyinfo.BPD_ITG * buyinfo.BPD_QTY;
                }
            }
            decimal ORDER_TAMT = ORDER_AMT + WLFY;
            ORDER_AAMT = ORDER_TAMT - ORDER_OFFERAMT;

            #region 构造订单主表信息



            Model.ORDERDOC orderdoc = new Model.ORDERDOC();
            orderdoc.ORDER_CUSTOMERCODE = gkdm;
            orderdoc.ORDER_NOTE = bz;
            orderdoc.ORDER_OFCODE = ddly;
            orderdoc.ORDER_APAY = Int32.Parse(jylx);
            orderdoc.ORDER_TRANERCODE = wlfs;
            orderdoc.ORDER_TRANCOST = WLFY;
            orderdoc.ORDER_COSTNATURE = fyxz;
            orderdoc.ORDER_GETER = address.Receiver;
            orderdoc.ORDER_GETERPHONE1 = address.Tel;
            orderdoc.ORDER_GETERPHONE2 = address.Mobile;
            orderdoc.ORDER_PROVINCE = address.Provice;
            orderdoc.ORDER_CITY = address.City;
            orderdoc.ORDER_AREA = address.Area;
            orderdoc.ORDER_GETADDRESS = address.Address;
            orderdoc.ORDER_GETPOSTCODE = address.PostDM;
            orderdoc.ORDER_QTY = Order_SL;
            orderdoc.ORDER_DISC = Order_ZK;
            orderdoc.ORDER_AMT = ORDER_AMT;
            orderdoc.ORDER_OFFERAMT = ORDER_OFFERAMT;
            orderdoc.ORDER_AAMT = ORDER_AAMT;
            orderdoc.ORDER_TAMT = ORDER_TAMT;
            orderdoc.ORDER_CREATER = Share.UserValue(0);
            orderdoc.ORDER_CREATEDATE = DateTime.Now;
            orderdoc.ORDER_ITGBUY = BPD_ITGBUY;
            orderdoc.ORDER_ITG = SYJF;
            orderdoc.ORDER_NINV = FP;
            orderdoc.ORDER_INVTITLE = FPTT;
            orderdoc.ORDER_OFFERTYPE = CXFS;
            orderdoc.ORDER_OFFERID = CXDH;
            orderdoc.ORDER_WEIGHT = div_spzzl.Value.Trim() == "" ? 0.00M : Decimal.Parse(div_spzzl.Value.Trim());
            orderdoc.ORDER_PAYAMT = payAmount;
            orderdoc.ORDER_NINV = Convert.ToInt32(isFp);
            orderdoc.ORDER_INVTITLE = tt;
            orderdoc.ORDER_PROVINCE = province;
            orderdoc.ORDER_CITY = city;
            orderdoc.ORDER_AREA = area;
            orderdoc.ORDER_IsPromotion = CXDH.Length > 0 ? 1 : 0;
            orderdoc.BYZD18 = "";  //存淘宝同步过来的订单编号
            #endregion
            //if (orderdoc.ORDER_DTCODE == "003")
            //{
            //    Model.V_VIPSET vvs = new BLL.V_VIPSET().GetModelByGKDM(orderdoc.ORDER_CUSTOMERCODE);
            //    if (vvs.BYZD13 < orderdoc.ORDER_AAMT)
            //    {
            //        result = "可用发货金额不足！,false"; return;
            //    }
            //}
            BLL.ORDERDOC bll_order = new BLL.ORDERDOC();
            //Share.Alert(Page, "可用发货金ss额不足！"); return;
            string str = bll_order.Order_Insert_HouTai(orderdoc, BuyLists, PayLists, 1);
            if (str == "")
            {
                result = "您填写的订单信息有误，请仔细检查核对有无错误信息和漏填信息！,false";
            }
            else
            {
                result = str + ",true";
            }
            
            #endregion

        }
        catch (FormatException ex)
        {
            result = "请输入正确的支付明细信息！,false";
        }
        catch
        {
            result = "意外错误！,false";
        }
    }

    public string GetCallbackResult()
    {
        return result;
    }
    #endregion

    #region 初始化物流公司



    private void Init_WLGS()
    {
        StringBuilder Sb = new StringBuilder();
        List<Model.TRANER> traners = new List<Model.TRANER>();
        traners = new BLL.TRANER().GetAllList(true);
        Sb.Append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        if (traners.Count > 0)
        {
            Sb.Append("<tr class=\"r_bgcolor5\">");
            Sb.Append("<td >");
            //Sb.Append("<input id=\"rbtExpress\" type=\"radio\" name=\"DeliverType\" value=\"rbtExpress\" onclick=\"SelectDeliverType(this)\"/> <label for=\"rbtExpress\">快递送货上门</label>");
            //Sb.Append("</td>");
            //Sb.Append("<td><span style=\"color: #FF0000\">支持货到付款.</span></td>");
            //Sb.Append("<td></td></tr>");
            //Sb.Append("<tr><td colspan=\"3\" align=\"center\">");
            Sb.Append("<div class=\"wliu\" id=\"Deliver\"");
            Sb.Append("<ul>");
            foreach (Model.TRANER traner in traners)
            {
                //if (traner.TRANER_AFTER == 1)
                //{
                    Sb.Append("<li style=\"text-align:left;list-style:none\">");
                    Sb.Append("<input id=\"rbt" + traner.TRANER_CODE + "\" type=\"radio\" name=\"Deliver\" onclick=\"CalculateYF(this)\" value=\"" + traner.TRANER_CODE + "\"/><label for=\"rbtExpress\">" + traner.TRANER_NAME + "</label>");
                    Sb.Append("</li>");
                //}
            }
            Sb.Append("</ul>");
            Sb.Append("</div>");
            Sb.Append("</td></tr>");
            //foreach (Model.TRANER traner in traners)
            //{
            //    if (traner.TRANER_AFTER == 0)
            //    {
            //        Sb.Append("<tr class=\"r_bgcolor5\">");
            //        Sb.Append("<td>");
            //        Sb.Append("<input id=\"rbt" + traner.TRANER_CODE + "\" type=\"radio\" name=\"DeliverType\" onclick=\"SelectDeliverType(this)\" value=\"" + traner.TRANER_CODE + "\"/> <label for=\"rbt" + traner.TRANER_CODE + "\">" + traner.TRANER_NAME + "</label>");
            //        Sb.Append("</td>");
            //        Sb.Append("<td><span style=\"color: #FF0000\">不支持货到付款。</span></td>");
            //        Sb.Append("<td></td>");
            //        Sb.Append("</tr>");
            //    }
            //}
        }
        else
        {
            Sb.Append("<tr class=\"r_bgcolor5\">");
            Sb.Append("<td>无物流信息，请联系客服! </td>");
            Sb.Append("</tr>");
        }
        Sb.Append("</table>");
        WLGS = Sb.ToString();
        //decimal mfje=decimal.Parse(ConfigurationManager.AppSettings["WLFREE"]);
        //if (mfje > 0)
        //{
        //    lbl_freestandard.Visible = true;
        //    lbl_freestandard.Text = "(货款满" + mfje.ToString() + "免运费)";
        //    Hid_Free.Value = mfje.ToString();
        //}
        //else
        //{
        //    lbl_freestandard.Visible = false;
        Hid_Free.Value = BLL.Common.GetFreeStandard().ToString();
        //}
    }
    #endregion

    
}
